<?php 
/**
 * 列表页控制器
 */
class ListController extends CommonController{
	/**
	 * 列表页首页
	 */
	public function index(){
		//问题库处理(顶级分类)
		$this->topCate();
		//累计提问数量
		$this->wenCate();
	    //历史回答问题最多的人
		$this->answerCate();
		//后盾问答助人光荣榜
		$this->acceptCate();
		//本日回答问题最多的人
        $this->todayCate();
		
		
		//获得子集分类
		$cid=(int)$_GET['cid'];
		$slq="SELECT * FROM hd_category WHERE pid={$cid}";
		$sonCate=M()->query($slq);
		if(!$sonCate){
			$pid = M()->query("SELECT pid FROM hd_category WHERE cid={$cid}");
			$pid = $pid[0]['pid'];
			$sonCate=M()->query("SELECT * FROM hd_category WHERE pid={$pid}");
			
		}
		$this->assign('sonCate',$sonCate);
		
		//处理面包屑导航
		$data = M()->query("SELECT * FROM hd_category");
		//获得父级分类
		$fatherCate = $this->getFather($data, $cid);
		//数组倒序
		$fatherCate = array_reverse($fatherCate);
        //分配变量
        $this->assign('fatherCate',$fatherCate);
		
		//处理不同情况显示不同问题
		$w = isset($_GET['w']) ? $_GET['w']	: 0;
		switch ($w) {
			//待解决
			case 0:
				$sql = "SELECT * FROM hd_ask AS a JOIN hd_category AS c ON a.cid=c.cid WHERE c.cid={$cid} AND solve=0";
				break;
			//已解决
			case 1:
				$sql = "SELECT * FROM hd_ask AS a JOIN hd_category AS c ON a.cid=c.cid WHERE c.cid={$cid} AND solve=1";
				break;
			//高悬赏
			case 2:
				$sql = "SELECT * FROM hd_ask AS a JOIN hd_category AS c ON a.cid=c.cid WHERE c.cid={$cid} AND reward>1";
				break;
			//零回答
			case 3:
				$sql = "SELECT * FROM hd_ask AS a JOIN hd_category AS c ON a.cid=c.cid WHERE c.cid={$cid} AND answer=0";
				break;
		}
		$askData = M()->query($sql);
		
		$this->assign('askData',$askData);
		
		$this->display();
	}
	
	
	private function getFather($data,$cid){
		 static $temp=array();
		 foreach ($data as $v) {
		 	if($v['cid']==$cid){
		 		$temp[] = $v;
				$this->getFather($data,$v['pid']);
		 	}
		 }
		 return $temp;
	}
	
}










